package l1j.server.server.datatables.sql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.DatabaseFactory;
import l1j.server.server.datatables.lock.GameConfigReading;
import l1j.server.server.datatables.storage.GameConfigStorage;
import l1j.server.server.templates.GameConfig;
import l1j.server.server.utils.SQLUtil;

public class GameConfigTable  implements GameConfigStorage{
	private static final Logger _log = Logger.getLogger(GameConfigReading.class.getName());
	
	private static final HashMap<Integer , GameConfig>_map = new HashMap<Integer , GameConfig>();
	
	@Override
	public void load() {
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			con = DatabaseFactory.get().getConnection();
			pstm = con.prepareStatement("SELECT * FROM z_opqlo_PcConfig ");
			rs = pstm.executeQuery();
			while (rs.next()) {
				final GameConfig value = new GameConfig();
				value.setTV(rs.getInt("b1"));
				value.setPartyIfo(rs.getInt("b2"));
				value.setPVPIfo(rs.getInt("b3"));
				value.setWeaponEffect(rs.getInt("b4"));
				value.setMagicScroll(rs.getInt("b5"));
				_map.put(rs.getInt("objid"), value);
			}
		} catch (final Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(rs, pstm, con);
		}
	}

	@Override
	public GameConfig getConfig(final int id) {
		if(!_map.containsKey(id)){
			this.addConfig(id);
			_map.put(id, new GameConfig());
		}
		return  _map.get(id);
	}

	
	private void addConfig(int id) {
		java.sql.Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = DatabaseFactory.get().getConnection();
			pstm = con.prepareStatement("INSERT INTO z_opqlo_PcConfig SET objid=?");
			pstm.setInt(1, id);
			pstm.execute();
		}catch (final Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}

	@Override
	public void updateB1(final int id, final int type) {
		if(!_map.containsKey(id)){
			return;
		}
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = DatabaseFactory.get().getConnection();
			pstm = con.prepareStatement("UPDATE z_opqlo_PcConfig SET b1= ? WHERE objid = ?");
			pstm.setInt(1, type);
			pstm.setInt(2, id);
			pstm.execute();
		} catch (final SQLException e) {
			_log.log(Level.SEVERE ,e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}
	@Override
	public void updateB2(final int id, final int type) {
		if(!_map.containsKey(id)){
			return;
		}
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = DatabaseFactory.get().getConnection();
			pstm = con.prepareStatement("UPDATE z_opqlo_PcConfig SET b2= ? WHERE objid = ?");
			pstm.setInt(1, type);
			pstm.setInt(2, id);
			pstm.execute();
		} catch (final SQLException e) {
			_log.log(Level.SEVERE ,e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}
	@Override
	public void updateB3(final int id, final int type) {
		if(!_map.containsKey(id)){
			return;
		}
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = DatabaseFactory.get().getConnection();
			pstm = con.prepareStatement("UPDATE z_opqlo_PcConfig SET b3= ? WHERE objid = ?");
			pstm.setInt(1, type);
			pstm.setInt(2, id);
			pstm.execute();
		} catch (final SQLException e) {
			_log.log(Level.SEVERE ,e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}
	@Override
	public void updateB4(final int id, final int type) {
		if(!_map.containsKey(id)){
			return;
		}
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = DatabaseFactory.get().getConnection();
			pstm = con.prepareStatement("UPDATE z_opqlo_PcConfig SET b4= ? WHERE objid = ?");
			pstm.setInt(1, type);
			pstm.setInt(2, id);
			pstm.execute();
		} catch (final SQLException e) {
			_log.log(Level.SEVERE ,e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}
	
	
	@Override
	public void deleteConfig(final int id){
		if(!_map.containsKey(id)){
			return;
		}
		_map.remove(id);
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = DatabaseFactory.get().getConnection();
			pstm = con.prepareStatement("DELETE FROM z_opqlo_PcConfig WHERE objid = ?");
			pstm.setInt(1, id);
			pstm.execute();
		} catch (final SQLException e) {
			_log.log(Level.SEVERE ,e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}

	@Override
	public void updateB5(int id, int type) {
		if(!_map.containsKey(id)){
			return;
		}
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = DatabaseFactory.get().getConnection();
			pstm = con.prepareStatement("UPDATE z_opqlo_PcConfig SET b5= ? WHERE objid = ?");
			pstm.setInt(1, type);
			pstm.setInt(2, id);
			pstm.execute();
		} catch (final SQLException e) {
			_log.log(Level.SEVERE ,e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}
	
	@Override
	public void updateB6(int id, int type) {
		if(!_map.containsKey(id)){
			return;
		}
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = DatabaseFactory.get().getConnection();
			pstm = con.prepareStatement("UPDATE z_opqlo_PcConfig SET b6= ? WHERE objid = ?");
			pstm.setInt(1, type);
			pstm.setInt(2, id);
			pstm.execute();
		} catch (final SQLException e) {
			_log.log(Level.SEVERE ,e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}

}
